package com.gui.beans;

/**
 * @author emylyano3
 *
 */

import javax.swing.*;
import javax.swing.filechooser.FileFilter;

import java.io.File;

/**
 * Creates a dialog window from where the user will be able to chose a file browsing the file system.
 * */
public class ImageFileSelector extends FileSelector {
	
	public static final String JPG = "jpg";
	public static final String GIF = "gif";
	public static final String PNG = "png";
	public static final String BMP = "bmp"; 
	
	private static final long serialVersionUID = 1L;
    
	public ImageFileSelector() {		
		
	}

    /**
     * Creates the file selection GUI and shows it.
     * <p>
     * If the user finds a supported image it return a file descriptor of that image.
     * If not it returns null.
     * <p>
     * @return File 
     */
    public File createAndShowGUI() {
        fs = new JFileChooser();
        fs.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fs.setAcceptAllFileFilterUsed(false);
        fs.setFileFilter(new ImageFileFilter());
        int returnVal = fs.showOpenDialog(ImageFileSelector.this);
        switch (returnVal) {
	        case JFileChooser.APPROVE_OPTION:
	        	if (this.isCorrectFile(fs.getSelectedFile()))
	        		return fs.getSelectedFile();
	        	else
	        		return null;
	        case JFileChooser.CANCEL_OPTION:
	        default: 
	        	return null;
        }
    }
    
	/**
	 * Checks if the image chosen by user is supported by the application.
	 * @return boolean 
	 * */
    @Override
	protected boolean isCorrectFile(File f) {
		// TODO Auto-generated method stub
		return true;
	}

	protected class ImageFileFilter extends FileFilter{

		@Override
		public boolean accept(File f) {
			return f.isDirectory() ||
			f.getName().toLowerCase().endsWith(PNG) ||
			f.getName().toLowerCase().endsWith(JPG) ||
			f.getName().toLowerCase().endsWith(GIF) ||
			f.getName().toLowerCase().endsWith(BMP);
		}

		@Override
		public String getDescription() {
			 return "Image files";
		}
    }

	
}
